using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Sharpie.Libs
{
    public class Textbox : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private KeyboardState key;
        private SpriteBatch batch;
        private SpriteFont font;
        private KeyboardStringReader textReader = new KeyboardStringReader();
        private StringBuilder inputText = new StringBuilder();
        private Texture2D blanktexture;
        private bool blink = true;

        public Textbox(Point point, int width, SpriteFont Font, Game game)
            : base(game)
        {
            font = Font;
            Frame = true;
            TextColor = Color.White;
            MaxLetters = 256;
            BackShow = true;
            Opacity = 0.3F;
            BackColor = Color.White;
            Width = width;
            Point = point;
            ShowCursor = true;
            BlinkingCursor = true;
            Initialize();
        }

        public bool Frame { get; set; }

        public Color TextColor { get; set; }

        public Color BackColor { get; set; }

        public Align TextAlign { get; set; }

        public bool BackShow { get; set; }

        public bool ShowCursor { get; set; }

        public bool BlinkingCursor { get; set; }

        public float Opacity { get; set; }

        public int MaxLetters
        {
            get { return textReader.MaxLength; }
            set { textReader.MaxLength = value; }
        }

        public int Width { get; set; }

        public Point Point { get; set; }

        public void SetText(string text)
        {
            inputText.Clear();
            inputText.Append(text);
        }

        public enum Align
        {
            Left,
            Center,
            Right
        }

        public string GetText() { return inputText.ToString(); }

        public override void Initialize()
        {
            base.Initialize();
            textReader.RepeatKeyInterval = 50;
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            batch = new SpriteBatch(GraphicsDevice);
            blanktexture = new Texture2D(GraphicsDevice, 1, 1);		//teksturki Rectangle
            blanktexture.SetData(new Color[] { Color.White });
        }

        private int time = 0;

        public override void Update(GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime.Milliseconds;
            key = Keyboard.GetState();
            this.textReader.Process(key, gameTime, this.inputText);
            if (font.MeasureString(inputText.ToString()).X > Width - 3)
                inputText.Remove(inputText.Length - 1, 1);
            base.Update(gameTime);
            if (BlinkingCursor)
            {
                if (time > 350)
                {
                    time = 0;
                    if (blink) blink = false;
                    else blink = true;
                }
            }
            else if (!blink) blink = true;
        }

        public override void Draw(GameTime gameTime)
        {
            batch.Begin();
            if (BackShow)
                batch.Draw(blanktexture, new Rectangle(Point.X - 5, Point.Y - 5, Width + 10, font.LineSpacing + 10), BackColor * Opacity);
            if (Frame)
            {
                batch.Draw(blanktexture, new Rectangle(Point.X - 5, Point.Y - 5, Width + 10, 1), Color.Black);
                batch.Draw(blanktexture, new Rectangle(Point.X - 5, Point.Y - 5, 1, font.LineSpacing + 10), Color.Black);
                batch.Draw(blanktexture, new Rectangle(Point.X + Width + 5, Point.Y - 5, 1, font.LineSpacing + 10), Color.Black);
                batch.Draw(blanktexture, new Rectangle(Point.X - 5, Point.Y + font.LineSpacing + 5, Width + 11, 1), Color.Black);
            }

            if (TextAlign == Align.Left)
            {
                Text.DrawShadowedText(batch, font, inputText.ToString(), new Vector2((int)(Point.X + 2), Point.Y), TextColor);
                if (ShowCursor & blink)
                    batch.Draw(blanktexture, new Rectangle((int)(Point.X + 4 + font.MeasureString(inputText.ToString()).X), Point.Y, 1, font.LineSpacing), Color.White);
            }
            else if (TextAlign == Align.Center)
            {
                Text.DrawShadowedText(batch, font, inputText.ToString(), new Vector2((int)(GetBoxRectangle().Center.X - font.MeasureString(inputText).X / 2), (int)Point.Y), TextColor);
                if (ShowCursor & blink)
                    batch.Draw(blanktexture, new Rectangle((int)(GetBoxRectangle().Center.X + font.MeasureString(inputText).X / 2), Point.Y, 1, font.LineSpacing), Color.White);
            }
            else
            {
                Text.DrawShadowedText(batch, font, inputText.ToString(), new Vector2(GetBoxRectangle().Right - (int)font.MeasureString(inputText).X - 6, Point.Y), TextColor);
                if (ShowCursor & blink)
                    batch.Draw(blanktexture, new Rectangle(GetBoxRectangle().Right - 6, Point.Y, 1, font.LineSpacing), Color.White);
            }
            batch.End();
            base.Draw(gameTime);
        }

        public Rectangle GetBoxRectangle()
        {
            return new Rectangle(Point.X - 5, Point.Y - 5, Width + 10, font.LineSpacing + 10);
        }
    }
}